<?php
class Pager
{
    function findStart($limit)////////lay trang hien hanh
    {   
        if((!isset($_GET['page'])) || ($_GET['page'] == "1") || !is_numeric($_GET['page']) )
        {
            $start = 0;
            $_GET['page'] = 1; /*trang đầu tiên */
        }
        else
        {
            $start = ($_GET['page']-1)*$limit;
        }
        return $start;
        
    }
    
    
    function findPages($count, $limit)///tong so trang
    {
        $pages = (($count % $limit) == 0)? $count / $limit:
        ceil($count / $limit); //lấy sàn của $count/$limit
        return $pages;
    }
    
    function pagesList($curpage, $pages, $configPage)
    {
    	if($pages == 1)
    		return null;
        $page_list = "<div ".$configPage["tag"]["pageNum"].">";
		$url = Tool::curPageURL();
		$check = strpos($url, "page");//check string : page
		if($check==true)
			$url = substr($url,0,$check-1);
		
		$check1 = strpos($url, "?");//check string : ?
		if($check1==true)
		{
			$item = "&";
		}
		else 
			$item ="?";

        if(($curpage != 1) && ($curpage))
        {
            $page_list .= "<a href='".$url.$item."page=1' ".$configPage["tag"]["firstLastPage"].">First Page</a>"." ";
        }
        
        if(($curpage-1) > 0)
        {
            $page_list .= "<a href='".$url.$item."page=".($curpage-1)."' ".$configPage["tag"]["firstLastPage"]." >Back  </a>"." ";
        }
        

        for ($i=1; $i<=$pages; $i++)
        {
            if ($i == $curpage)
            {
                $page_list .= "<span ".$configPage["tag"]["curPage"].">".$i."</span>";
            }
            else
            {
                $page_list .= "<a href='".$url. $item."page=".$i."'> ".$i."</a>";
            }
            $page_list .=" ";
        }
        
        // In link của trang tiếp theo và trang cuối cùng 
        
        
        if (($curpage+1) <= $pages)
        {
            $page_list .= "<a href='".$url.$item."page=".($curpage+1)."' ".$configPage["tag"]["firstLastPage"]."> Next</a> &nbsp;";
        }
        
            if (($curpage != $pages) && ($pages != 0) )
        {
            $page_list .= "<a href=\"".$url.$item."page=".$pages."\" ".$configPage["tag"]["firstLastPage"].">Last Page</a>";
        }
    
        $page_list .= "</div>\n";
           
        return $page_list;
    }
    
}  
?> 